<template>
  <div class="categories">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addShop()">添加商品</el-button>
        </el-col>
      </el-row>
      <tree-table
        show-index
        index-text="#"
        :selection-type="false"
        :expand-type="false"
        :data="resultlist"
        :columns="columns"
        border
        v-loading="loading"
      >
        <template slot="Effective" slot-scope="scope">
          <i class="el-icon-success" v-if="!scope.row.cat_deleted" style="color:green"></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <template slot="Grade" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level == 1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <template slot="operation" slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </tree-table>

      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="quaryInfo.pagenum"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="quaryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <el-dialog title="添加商品" :visible.sync="shopVisible" width="60%" @close="resetAddShop">
      <el-form
        :model="ruleShop"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleShop.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="cascaderValue"
            :options="cascaderList"
            :props="resultprops"
            clearable
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="shopVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate()">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改商品" :visible.sync="xiugaiVisible" width="60%" @close="resetXiugaiShop()">
      <el-form :model="ruleXiuShop" :rules="Xiurules" ref="ruleFormXiu" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="ruleXiuShop.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="xiugaiVisible = false">取 消</el-button>
        <el-button type="primary" @click="xiuCate()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "categories",
  data() {
    return {
      loading: true,
      quaryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 10,
      },
      resultlist: [],
      total: null,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "Effective",
        },
        {
          label: "等级",
          type: "template",
          template: "Grade",
        },
        {
          label: "操作",
          type: "template",
          template: "operation",
        },
      ],
      shopVisible: false,
      ruleShop: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0,
      },
      rules: {
        cat_name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
      ruleXiuShop: {
        cat_name: "",
      },
      Xiurules: {
        cat_name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
      resultprops: {
        checkStrictly: true,
        expandTrigger: "hover",
        label: "cat_name",
        value: "cat_id",
      },
      cascaderList: [],
      cascaderValue: [],
      xiugaiVisible: false,
      cat_id: null,
    };
  },
  methods: {
    getTableTree() {
      this.loading = true;
      this.$http.get("categories", { params: this.quaryInfo }).then((res) => {
        // if(res.data.status )
        if (res.data.meta.status !== 200) {
          this.$message.error("数据加载失败");
        }
        this.resultlist = res.data.data.result;
        this.total = res.data.data.total;
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      this.quaryInfo.pagesize = val;
      this.getTableTree();
    },
    handleCurrentChange(val) {
      this.quaryInfo.pagenum = val;
      this.getTableTree();
    },
    addShop() {
      this.$http.get("categories", { params: { type: 2 } }).then((res) => {
        if (res.data.meta.status !== 200) {
          this.$message.error("数据加载失败");
        }
        this.cascaderList = res.data.data;
        console.log(res);
      });
      this.shopVisible = true;
    },
    resetAddShop() {
      this.$refs.ruleForm.resetFields();
      this.cascaderValue = [];
    },
    handleChange(value) {
      console.log(this.cascaderValue);
      if (value.length > 0) {
        this.ruleShop.cat_pid = value[value.length - 1];
        this.ruleShop.cat_level = value.length;
        // console.log(this.ruleShop.cat_pid )
      } else {
        this.ruleShop.cat_pid = 0;
        this.ruleShop.cat_level = 0;
      }
      //console.log(value);
    },
    addCate() {
      this.$refs.ruleForm.validate((validate) => {
        if (!validate) {
          return;
        }
        this.$http.post("categories", this.ruleShop).then((res) => {
          if (res.data.meta.status !== 201) {
            return this.$message.error("添加商品失败");
          }
          this.$message.success("添加商品成功");

          this.shopVisible = false;
          this.getTableTree();
        });
      });
    },
    handleEdit(scope) {
      this.cat_id = scope.cat_id;
      this.$http.get(`categories/${this.cat_id}`).then((res) => {
        if (res.data.meta.status == 200) {
          this.ruleXiuShop.cat_name = res.data.data.cat_name;
        }
      });
      this.xiugaiVisible = true;
    },
    resetXiugaiShop() {
      this.$refs.ruleFormXiu.resetFields();
    },
    xiuCate() {
      this.$http
        .put(`categories/${this.cat_id}`, {
          cat_name: this.ruleXiuShop.cat_name,
        })
        .then((res) => {
          if (res.data.meta.status !== 200) {
            return this.$message.error("修改失败");
          }
          this.$message.success("修改成功");
          this.getTableTree();
          this.xiugaiVisible = false;
        });
    },
    handleDelete(scope) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete(`categories/${scope.cat_id}`).then((res) => {
            if (res.data.meta.status !== 200) {
              return this.$message.error("删除失败");
            }
            this.$message.success("删除成功");
            this.getTableTree();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    this.getTableTree();
  },
};
</script>

<style scoped>
.el-breadcrumb {
  margin-bottom: 20px;
}
.el-row {
  margin-bottom: 10px;
}
.el-pagination {
  margin-top: 15px;
}
</style>
